package com.gitee.zeric.codec.generaters;

import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSONObject;
import com.gitee.zeric.codec.exception.ParamErrorException;
import com.gitee.zeric.codec.util.VUtils;

import java.util.Arrays;

/**
 * @author zj
 * @date 2021/12/16
 */
public class LetterNumberGenerater extends AbstractCodeGerator {
    @Override
    public String generate(String ruleCode, JSONObject param) {
        RuleParam ruleParam = trans(RuleParam.class, param);
        if (ruleParam.getUpperCase() == 0) {
            //小写字母+数字
            return RandomUtil.randomStringUpper(ruleParam.getSize());
        }else if(ruleParam.getUpperCase()==1){
            return RandomUtil.randomString(ruleParam.getSize());
        }
        return RandomUtil.randomString("abcdefghigklmnopqrstuvwxyz" +
                "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
                +"123456789", ruleParam.getSize());
    }

    @Override
    protected String[] fields() {
        return new String[]{"size", "upperCase"};
    }

    @Override
    public void checkParam(JSONObject paramJson) {
        super.checkParam(paramJson);
        RuleParam ruleParam = trans(RuleParam.class, paramJson);
        try {
            Integer upperCase = ruleParam.getUpperCase();
            VUtils.isTure(!Arrays.asList(0, 1, 2).contains(upperCase)).throwMessage("请检查字母大小写参数！");
        } catch (Exception e) {
            throw new ParamErrorException("请检查upperCase字段值类型是否为数字！");
        }
    }
}
